#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#define random(x) (rand()%x)
#include<windows.h>
int high,width;		//ÓÎÏ·»­Ãæ³ß´ç
int position_x,position_y;		//·É»úÎ»ÖÃ
int bullet_x,bullet_y;		//×Óµ¯Î»ÖÃ 
int enemy_x,enemy_y;		//µÐ»úÎ»ÖÃ 
int score;		//µÃ·Ö 
int g;		//¿ØÖÆËÙ¶È 
 
void gotoxy(int x,int y)	//¹â±êÒÆ¶¯µ½£¨x,y£©Î»ÖÃ 
{
	COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
} 

void hide()         //Òþ²Ø¹â±ê
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
}


void starup()	//Êý¾Ý³õÊ¼»¯
{
	high=20;
	width=32;
	position_x=high/2;
	position_y=width/2;
	bullet_y=position_y;
	bullet_x=-1;
	enemy_x=0;
	enemy_y=width/2;
	score=0; 
	g=50; 
	hide(); 
	
}

void show()		//ÏÔÊ¾»­Ãæ
{
    int i,j;
    gotoxy(0,0);      //ÇåÆÁ
    for(i=0;i<high;i++)
    {
	    for(j=0;j<width;j++)
        {
            if((i==position_x)&&(j==position_y)||(i==position_x+2)&&(j==position_y-1)||(i==position_x+2)&&(j==position_y+1))
                printf("*");        //Êä³ö·É»ú
            else if((i==position_x+1)&&(j==position_y-2)||(i==position_x+1)&&(j==position_y-1)||(i==position_x+1)&&(j==position_y)) 
            	printf("*");
            else if((i==position_x+1)&&(j==position_y+1)||(i==position_x+1)&&(j==position_y+2)) 
            	printf("*");
            else if((i==bullet_x)&&(j==bullet_y))
                printf("|");		//Êä³ö×Óµ¯ 
            else if((i==enemy_x)&&(j==enemy_y))
            	printf("+");		//Êä³öµÐ»ú     
			else if(i==0)		//±ß¿ò 
            	printf("*");
            else if(i==high-1)
            	printf("*");
			else if(j==width-1)
				printf("*");         
			else
                printf(" ");        //Êä³ö¿Õ¸ñ
        }
        printf("\n");
	}
	printf("µÃ·Ö£º%d\n",score); 
}

void updateWithoutInput()		//ÓëÓÃ»§ÊäÈëÎÞ¹ØµÄ¸üÐÂ
{
	if(bullet_x>-1)
		bullet_x--;
	static int speed=0;		//¿ØÖÆµÐ»úËÙ¶È 
	if(speed<g)
		speed++;
	if(enemy_x>high)
	{
		enemy_x=0;
		enemy_y=rand()%28+3;
	}
	else
	{
		if(speed==g) 
		{
			enemy_x++; 	
			speed=0; 
		}	
	} 
	if((bullet_x==enemy_x)&&(bullet_y==enemy_y))	//»÷ÖÐµÐ»ú 
	{
		score++;
		if(g>=1)
			g--;		//ÊÇµÐ»úËÙ¶È±ä¿ì 
		enemy_x=0;
		enemy_y=rand()%25+3;
		bullet_y=-1;
	}
}

void updateWithInput()		//ÓëÓÃ»§ÊäÈëÓÐ¹ØµÄ¸üÐÂ
{
	char input;
	if(kbhit()) 		//µ±°´¼üÊ±Ö´ÐÐ 
	{
		input=getch();			//ÓÃ»§Êä³ö¼°·ÀÖ¹³¬³ö±ß½ç 
		if((input=='a')&&(position_y-2>=1))
			position_y--; 
		if((input=='d')&&(position_y+2<=width-3))
			position_y++;
		if((input=='w')&&(position_x>=2))
			position_x--;
		if((input=='s')&&(position_x+2<=high-3))
			position_x++;   
		if(input==' ')
		{
			bullet_x=position_x-1;
			bullet_y=position_y;
		} 
		if(input==27)
		{
			system("pause"); 
			system("cls");
		}
	} 
}

int main()
{
    starup();   //Êý¾Ý³õÊ¼»¯
    while(1)    //ÓÎÏ·Ñ­»·Ö´ÐÐ
    {
        show(); //ÏÔÊ¾»­Ãæ
        updateWithoutInput();   //ÓëÓÃ»§ÊäÈëÎÞ¹ØµÄ¸üÐÂ
        updateWithInput();  //ÓëÓÃ»§ÊäÈëÓÐ¹ØµÄ¸üÐÂ
    }
    return 0;
}